<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="utf-8" />
    
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
  <title>
    Mybatis核心知识整理 |  Yolin
  </title>
  <meta name="generator" content="hexo-theme-yilia-plus">
  
  <link rel="shortcut icon" href="/favicon.png" />
  
  
<link rel="stylesheet" href="/css/style.css">

  
<script src="/js/pace.min.js"></script>


  

  

<link rel="alternate" href="/atom.xml" title="Yolin" type="application/atom+xml">
</head>

</html>

<body>
  <div id="app">
    <main class="content">
      <section class="outer">
  <article id="post-MyBatis核心知识整理" class="article article-type-post" itemscope
  itemprop="blogPost" data-scroll-reveal>

  <div class="article-inner">
    
    <header class="article-header">
       
<h1 class="article-title sea-center" style="border-left:0" itemprop="name">
  Mybatis核心知识整理
</h1>
  

    </header>
    

    
    <div class="article-meta">
      <a href="/2021/02/10/MyBatis%E6%A0%B8%E5%BF%83%E7%9F%A5%E8%AF%86%E6%95%B4%E7%90%86/" class="article-date">
  <time datetime="2021-02-10T10:45:02.000Z" itemprop="datePublished">2021-02-10</time>
</a>
      
  <div class="article-category">
    <a class="article-category-link" href="/categories/MyBatis/">MyBatis</a>
  </div>

      
      
<div class="word_count">
    <span class="post-time">
        <span class="post-meta-item-icon">
            <i class="ri-quill-pen-line"></i>
            <span class="post-meta-item-text"> 字数统计:</span>
            <span class="post-count">1.2k字</span>
        </span>
    </span>

    <span class="post-time">
        &nbsp; | &nbsp;
        <span class="post-meta-item-icon">
            <i class="ri-book-open-line"></i>
            <span class="post-meta-item-text"> 阅读时长≈</span>
            <span class="post-count">4分钟</span>
        </span>
    </span>
</div>

      
    </div>
    

    
    
    <div class="tocbot"></div>





    

    <div class="article-entry" itemprop="articleBody">
      


      

      
      <h2 id="MyBatis"><a href="#MyBatis" class="headerlink" title="MyBatis"></a>MyBatis</h2><blockquote>
<p>  MyBatis是一款优秀的持久层框架，它支持自定义SQL、存储过程以及高级映射，免除了几乎所有的jdbc代码以及设置参数和获取结果集的工作，它可以通过简单的xml或注解来配置java对象作为数据库中的记录。</p>
</blockquote>
<h2 id="MyBatis的编程步骤"><a href="#MyBatis的编程步骤" class="headerlink" title="MyBatis的编程步骤"></a>MyBatis的编程步骤</h2><ol>
<li>创建SqlSessionFactory</li>
<li>通过SqlSessionFactory创建SqlSession</li>
<li>通过SqlSession执行数据库操作</li>
<li>调用session.commit()提交事务</li>
<li>调用session.close()关闭对话</li>
</ol>
<h2 id="MyBatis工作原理"><a href="#MyBatis工作原理" class="headerlink" title="MyBatis工作原理"></a>MyBatis工作原理</h2><ol>
<li>读取MyBatis配置文件（mybatis-config.xml）.它配置了MyBatis的运行环境等信息，例如数据库连接信息</li>
<li>加载映射文件，即SQL映射文件，改文件配置了操作数据库的sql语句</li>
<li>构造会话工厂SQLSessionFactory</li>
<li>根据会话工厂创建会话对象sqlSession，该对象包含了执行sql语句的所有方法</li>
<li>Executor执行器，MyBatis底层定义了一个Executor接口来操作数据库，它根据sqlSession传递的参数动态地生成需要执行的sql语句</li>
<li>MappedStatement对象，用于对映射信息的封装用于存储要映射的sql语句的id，参数等</li>
<li>输入参数映射，输入参数类型可以是Map、List等集合类型，也可以是基本数据类型</li>
<li>输出结果映射。</li>
</ol>
<a id="more"></a>
<h2 id="MyBatis中的执行器Executor"><a href="#MyBatis中的执行器Executor" class="headerlink" title="MyBatis中的执行器Executor"></a>MyBatis中的执行器Executor</h2><ul>
<li>SimpleExecutor（默认）:每执行一次update或select，就开启一个Statement对象，用完立刻关闭Statement对象。</li>
<li>ReuseExecutor：重复使用Statement对象。以sql作为key查找Statement对象，存在就使用，不存在就创建，用完后，不关闭Statement对象供下次使用。</li>
<li>BatchExecutor：将所有sql都添加到批处理中，等待统一执行。</li>
</ul>
<h2 id="和-的区别"><a href="#和-的区别" class="headerlink" title="#{}和${}的区别"></a>#{}和${}的区别</h2><ul>
<li>#{}是占位符，预编译处理；${}是拼接符，字符串替换</li>
<li>Mybatis在处理#{}时，会将sql中的#{}替换为？号，调用PreparedStatement的set方法来赋值</li>
<li>#{}可防止sql注入，提高系统的安全性</li>
</ul>
<h2 id="模糊查询like语句怎么写"><a href="#模糊查询like语句怎么写" class="headerlink" title="模糊查询like语句怎么写"></a>模糊查询like语句怎么写</h2><ol>
<li><a href="#">“%”#{value}”%”</a> 注意，因为#{}解析成sql时，会在变量外侧自动加单引号，所以只能用双引号</li>
<li>推荐使用CONCAT函数，<a href="#">CONCAT(‘%’,#{value},’%’)</a></li>
</ol>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">select</span> <span class="attr">id</span>=<span class="string">"queryBookByName"</span> <span class="attr">resultType</span>=<span class="string">"Books"</span>&gt;</span></span><br><span class="line">    select * from books where bookName like "%"#&#123;bookName&#125;"%";</span><br><span class="line"><span class="tag">&lt;/<span class="name">select</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h2 id="mapper中传递参数的方法"><a href="#mapper中传递参数的方法" class="headerlink" title="mapper中传递参数的方法"></a>mapper中传递参数的方法</h2><blockquote>
<h3 id="1-Java-Bean传参"><a href="#1-Java-Bean传参" class="headerlink" title="1. Java Bean传参"></a>1. Java Bean传参</h3></blockquote>
<p><img src="https://yolin121.oss-cn-beijing.aliyuncs.com/blogimg/20210209151544.png" alt=""></p>
<blockquote>
<h3 id="2-Param注解传参"><a href="#2-Param注解传参" class="headerlink" title="2. @Param注解传参"></a>2. @Param注解传参</h3></blockquote>
<p><img src="https://yolin121.oss-cn-beijing.aliyuncs.com/blogimg/20210209200359.png" alt=""></p>
<blockquote>
<h3 id="3-Map传参法"><a href="#3-Map传参法" class="headerlink" title="3. Map传参法"></a>3. Map传参法</h3></blockquote>
<figure class="highlight"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">public</span> User <span class="title">selectUser</span><span class="params">(Map&lt;String, Object&gt; params)</span></span>;</span><br><span class="line"></span><br><span class="line">&lt;select id=<span class="string">"selectUser"</span> parameterType=<span class="string">"java.util.Map"</span></span><br><span class="line">resultMap=<span class="string">"UserResultMap"</span>&gt; </span><br><span class="line">    select * from user </span><br><span class="line">    where user_name = #&#123;userName&#125; and dept_id = #&#123;deptId&#125;</span><br><span class="line">&lt;/select&gt;</span><br></pre></td></tr></table></figure>
<p>#{} 里面的名称对应的是Map里的key</p>
<h2 id="Mapper编写的方式"><a href="#Mapper编写的方式" class="headerlink" title="Mapper编写的方式"></a>Mapper编写的方式</h2><h3 id="使用Mapper扫描器"><a href="#使用Mapper扫描器" class="headerlink" title="使用Mapper扫描器"></a>使用Mapper扫描器</h3><ol>
<li>mapper.xml文件的编写<ol>
<li>mapper.xml中的namespace为mapper接口的地址</li>
<li>mapper接口中的方法名和mapper.xml中的定义的statement的id保持一致</li>
</ol>
</li>
<li>定义mapper接口（与mapper.xml名称一致且在同一目录）</li>
<li>配置mapper扫描器</li>
</ol>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!--配置扫描Dao接口包，动态实现Dao接口注入到spring容器中--&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">bean</span> <span class="attr">class</span>=<span class="string">"org.mybatis.spring.mapper.MapperScannerConfigurer"</span>&gt;</span></span><br><span class="line">    <span class="comment">&lt;!--注入sqlSessionFactory--&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">"sqlSessionFactoryBeanName"</span> <span class="attr">value</span>=<span class="string">"sqlSessionFactory"</span>/&gt;</span></span><br><span class="line">    <span class="comment">&lt;!--给出需要扫描Dao接口包--&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">"basePackage"</span> <span class="attr">value</span>=<span class="string">"xyz.yolin.mapper"</span>/&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">bean</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h2 id="什么是MyBatis-的接口绑定？"><a href="#什么是MyBatis-的接口绑定？" class="headerlink" title="什么是MyBatis 的接口绑定？"></a>什么是MyBatis 的接口绑定？</h2><p>接口绑定，就是接口里的方法和sql语句绑定，我们直接调用方法就可以了</p>
<h3 id="有哪些实现方式？"><a href="#有哪些实现方式？" class="headerlink" title="有哪些实现方式？"></a>有哪些实现方式？</h3><ol>
<li>通过注解绑定，在接口上加上@Select、@Update等注解</li>
<li>xml配置来绑定，需要指定xml映射文件里的namespace为接口的全路径名</li>
</ol>
<p>sql语句简单时用注解，复杂时用xml绑定</p>
<h3 id="MyBatis中不同的xml映射文件，id是否可以重复"><a href="#MyBatis中不同的xml映射文件，id是否可以重复" class="headerlink" title="MyBatis中不同的xml映射文件，id是否可以重复"></a>MyBatis中不同的xml映射文件，id是否可以重复</h3><ul>
<li>如果配置了namespace，则id可以重复，如果没有配置namespace，则不能重复。</li>
<li>原因就是Map&lt;String,MappedStatement&gt;的key是namespace+id，有了namespace，自然id可以重复</li>
</ul>
<h3 id="动态sql"><a href="#动态sql" class="headerlink" title="动态sql"></a>动态sql</h3><p>MyBatis的动态sql可以让我们在xml映射文件内，<strong>以标签的形式编写动态sql，完成逻辑判断和动态拼接sql的功能</strong>，MyBatis提供多种动态sql标签：</p>
<pre><code>trim、where、set、foreach、if、choose、when、otherwise、bind</code></pre><h3 id="Mybatis一级、二级缓存"><a href="#Mybatis一级、二级缓存" class="headerlink" title="Mybatis一级、二级缓存"></a>Mybatis一级、二级缓存</h3><ol>
<li>一级缓存：基于PerpetualCache的HahsMap本地缓存，其存储作用域为Session，当Session flush或close之后，该Session中的所有Cache就将清空</li>
<li>二级缓存与一级缓存机制相同，默认也是采用PerpetualCache，HashMap存储，不同在于存储作用域为Mapper（namespace），且可以自定义存储源</li>
</ol>
<h3 id="MyBatis分页"><a href="#MyBatis分页" class="headerlink" title="MyBatis分页"></a>MyBatis分页</h3><p>使用分页插件pageHelper</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>com.github.pagehelper<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>pagehelper<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">version</span>&gt;</span>5.1.11<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p><img src="https://yolin121.oss-cn-beijing.aliyuncs.com/blogimg/20210210135737.png" alt=""></p>
<p><img src="https://yolin121.oss-cn-beijing.aliyuncs.com/blogimg/20210210180313.png" alt=""></p>
<p>分页插件的基本原理是使用MyBatis提供的插件接口，实现自定义插件，在插件拦截方法内拦截待执行的sql，然后重写sql，根据dialect方言，添加对应的物理分页语句和分页参数</p>

      
      <!-- reward -->
      
    </div>
      <!-- copyright -->
      
    <footer class="article-footer">
      
      
  <ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/MyBatis/" rel="tag">MyBatis</a></li></ul>


    </footer>

  </div>

  
  
  <nav class="article-nav">
    
      <a href="/2021/02/13/%E5%BE%AE%E4%BF%A1%E5%B0%8F%E7%A8%8B%E5%BA%8F%E3%80%8A%E6%97%A5%E5%B8%B8%E3%80%8B/" class="article-nav-link">
        <strong class="article-nav-caption">上一篇</strong>
        <div class="article-nav-title">
          
            微信小程序《日常》
          
        </div>
      </a>
    
    
      <a href="/2021/02/06/Redis%E4%BA%8B%E5%8A%A1%E5%8F%8A%E5%AE%9E%E7%8E%B0%E4%B9%90%E8%A7%82%E9%94%81/" class="article-nav-link">
        <strong class="article-nav-caption">下一篇</strong>
        <div class="article-nav-title">Redis事务及实现乐观锁</div>
      </a>
    
  </nav>


  

  
  
<!-- valine评论 -->
<div id="vcomments-box">
    <div id="vcomments">
    </div>
</div>
<script src="//cdn1.lncld.net/static/js/3.0.4/av-min.js"></script>
<script src='https://cdn.jsdelivr.net/npm/valine@1.3.10/dist/Valine.min.js'></script>
<script>
    new Valine({
        el: '#vcomments',
        notify: false,
        verify: '',
        app_id: '',
        app_key: '',
        path: window.location.pathname,
        avatar: 'mp',
        placeholder: '给我的文章加点评论吧~',
        recordIP: true
    });
    const infoEle = document.querySelector('#vcomments .info');
    if (infoEle && infoEle.childNodes && infoEle.childNodes.length > 0) {
        infoEle.childNodes.forEach(function (item) {
            item.parentNode.removeChild(item);
        });
    }
</script>
<style>
    #vcomments-box {
        padding: 5px 30px;
    }

    @media screen and (max-width: 800px) {
        #vcomments-box {
            padding: 5px 0px;
        }
    }

    #vcomments-box #vcomments {
        background-color: #fff;
    }

    .v .vlist .vcard .vh {
        padding-right: 20px;
    }

    .v .vlist .vcard {
        padding-left: 10px;
    }
</style>

  

  
  
  

</article>
</section>
      <footer class="footer">
  <div class="outer">
    <ul class="list-inline">
      <li>
        &copy;
        2020-2021
        张金
      </li>
      <li>
        
      </li>
    </ul>
    <ul class="list-inline">
      <li>
        
        
        <span>
  <i>PV:<span id="busuanzi_value_page_pv"></span></i>
  <i>UV:<span id="busuanzi_value_site_uv"></span></i>
</span>
        
      </li>
      <li>
        <!-- cnzz统计 -->
        
        <script type="text/javascript" src='https://s9.cnzz.com/z_stat.php?id=1278069914&amp;web_id=1278069914'></script>
        
        <img src="https://yolin121.oss-cn-beijing.aliyuncs.com/blogimg/beian.png">
        <a href="https://beian.miit.gov.cn"  style="color:#f72b07" target="_blank">陕ICP备20002449号</a>
      </li>

    </ul>
  </div>
</footer>
    <div class="to_top">
        <div class="totop" id="totop">
  <i class="ri-arrow-up-line"></i>
</div>
      </div>
    </main>
      <aside class="sidebar">
        <button class="navbar-toggle"></button>
<nav class="navbar">
  
  <div class="logo">
    <a href="/"><img src="/images/ayer-side.svg" alt="Yolin"></a>
  </div>
  
  <ul class="nav nav-main">
    
    <li class="nav-item">
      <a class="nav-item-link" href="/">主页</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/archives">归档</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/categories">分类</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/tags">标签</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/playlist">歌单</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/video">视频</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/about">关于我</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/friends">友情链接</a>
    </li>
    
  </ul>
</nav>
<nav class="navbar navbar-bottom">
  <ul class="nav">
    <li class="nav-item">
      
      <a class="nav-item-link nav-item-search"  title="Search">
        <i class="ri-search-line"></i>
      </a>
      
      
      <a class="nav-item-link" target="_blank" href="/atom.xml" title="RSS Feed">
        <i class="ri-rss-line"></i>
      </a>
      
    </li>
  </ul>
</nav>
<div class="search-form-wrap">
  <div class="local-search local-search-plugin">
  <input type="search" id="local-search-input" class="local-search-input" placeholder="Search...">
  <div id="local-search-result" class="local-search-result"></div>
</div>
</div>
      </aside>
      <div id="mask"></div>

<!-- #reward -->
<div id="reward">
  <span class="close"><i class="ri-close-line"></i></span>
  <p class="reward-p"><i class="ri-cup-line"></i>请我喝杯咖啡吧~</p>
  <div class="reward-box">
    
    <div class="reward-item">
      <img class="reward-img" src="/images/alipay.jpg">
      <span class="reward-type">支付宝</span>
    </div>
    
    
    <div class="reward-item">
      <img class="reward-img" src="/images/wechat.jpg">
      <span class="reward-type">微信</span>
    </div>
    
  </div>
</div>
      
<script src="/js/jquery-2.0.3.min.js"></script>


<script src="/js/jquery.justifiedGallery.min.js"></script>


<script src="/js/lazyload.min.js"></script>


<script src="/js/busuanzi-2.3.pure.min.js"></script>


<script src="/js/share.js"></script>




<script>
  try {
    var typed = new Typed("#subtitle", {
    strings: ['肠断月明红豆蔻,','月似当时,','人似当时否？'],
    startDelay: 1500,
    typeSpeed: 200,
    loop: true,
    backSpeed: 40,
    showCursor: true
    });
  } catch (err) {
  }
  
</script>




<script src="/js/tocbot.min.js"></script>

<script>
  // Tocbot_v4.7.0  http://tscanlin.github.io/tocbot/
  tocbot.init({
    tocSelector: '.tocbot',
    contentSelector: '.article-entry',
    headingSelector: 'h1, h2, h3, h4, h5, h6',
    hasInnerContainers: true,
    scrollSmooth: true,
    scrollContainer:'main',
    positionFixedSelector: '.tocbot',
    positionFixedClass: 'is-position-fixed',
    fixedSidebarOffset: 'auto',
    onClick: (e) => {
      $('.toc-link').removeClass('is-active-link');
      $(`a[href=${e.target.hash}]`).addClass('is-active-link');
      $(e.target.hash).scrollIntoView();
      return false;
    }
  });
</script>


<script>
  var ayerConfig = {
    mathjax: true
  }
</script>


<script src="/js/ayer.js"></script>


<script src="https://cdn.jsdelivr.net/npm/jquery-modal@0.9.2/jquery.modal.min.js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/jquery-modal@0.9.2/jquery.modal.min.css">


<!-- Root element of PhotoSwipe. Must have class pswp. -->
<div class="pswp" tabindex="-1" role="dialog" aria-hidden="true">

    <!-- Background of PhotoSwipe. 
         It's a separate element as animating opacity is faster than rgba(). -->
    <div class="pswp__bg"></div>

    <!-- Slides wrapper with overflow:hidden. -->
    <div class="pswp__scroll-wrap">

        <!-- Container that holds slides. 
            PhotoSwipe keeps only 3 of them in the DOM to save memory.
            Don't modify these 3 pswp__item elements, data is added later on. -->
        <div class="pswp__container">
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
        </div>

        <!-- Default (PhotoSwipeUI_Default) interface on top of sliding area. Can be changed. -->
        <div class="pswp__ui pswp__ui--hidden">

            <div class="pswp__top-bar">

                <!--  Controls are self-explanatory. Order can be changed. -->

                <div class="pswp__counter"></div>

                <button class="pswp__button pswp__button--close" title="Close (Esc)"></button>

                <button class="pswp__button pswp__button--share" style="display:none" title="Share"></button>

                <button class="pswp__button pswp__button--fs" title="Toggle fullscreen"></button>

                <button class="pswp__button pswp__button--zoom" title="Zoom in/out"></button>

                <!-- Preloader demo http://codepen.io/dimsemenov/pen/yyBWoR -->
                <!-- element will get class pswp__preloader--active when preloader is running -->
                <div class="pswp__preloader">
                    <div class="pswp__preloader__icn">
                        <div class="pswp__preloader__cut">
                            <div class="pswp__preloader__donut"></div>
                        </div>
                    </div>
                </div>
            </div>

            <div class="pswp__share-modal pswp__share-modal--hidden pswp__single-tap">
                <div class="pswp__share-tooltip"></div>
            </div>

            <button class="pswp__button pswp__button--arrow--left" title="Previous (arrow left)">
            </button>

            <button class="pswp__button pswp__button--arrow--right" title="Next (arrow right)">
            </button>

            <div class="pswp__caption">
                <div class="pswp__caption__center"></div>
            </div>

        </div>

    </div>

</div>

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe.min.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/default-skin/default-skin.css">
<script src="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe-ui-default.min.js"></script>

<script>
    function viewer_init() {
        let pswpElement = document.querySelectorAll('.pswp')[0];
        let $imgArr = document.querySelectorAll(('.article-entry img:not(.reward-img)'))

        $imgArr.forEach(($em, i) => {
            $em.onclick = () => {
                // slider展开状态
                // todo: 这样不好，后面改成状态
                if (document.querySelector('.left-col.show')) return
                let items = []
                $imgArr.forEach(($em2, i2) => {
                    let img = $em2.getAttribute('data-idx', i2)
                    let src = $em2.getAttribute('data-target') || $em2.getAttribute('src')
                    let title = $em2.getAttribute('alt')
                    // 获得原图尺寸
                    const image = new Image()
                    image.src = src
                    items.push({
                        src: src,
                        w: image.width || $em2.width,
                        h: image.height || $em2.height,
                        title: title
                    })
                })
                var gallery = new PhotoSwipe(pswpElement, PhotoSwipeUI_Default, items, {
                    index: parseInt(i)
                });
                gallery.init()
            }
        })
    }
    viewer_init()
</script>



<script type="text/x-mathjax-config">
  MathJax.Hub.Config({
      tex2jax: {
          inlineMath: [ ['$','$'], ["\\(","\\)"]  ],
          processEscapes: true,
          skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']
      }
  });

  MathJax.Hub.Queue(function() {
      var all = MathJax.Hub.getAllJax(), i;
      for(i=0; i < all.length; i += 1) {
          all[i].SourceElement().parentNode.className += ' has-jax';
      }
  });
</script>

<script src="https://cdn.jsdelivr.net/npm/mathjax@2.7.6/unpacked/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>


<script type="text/javascript" src="https://js.users.51.la/20544303.js"></script>
  </div>
</body>

</html>